#include "TemplatexPLDAO.h"

TemplatexPLDAO::TemplatexPLDAO()
{
    loaded = false;
}

TemplatexPLDAO::~TemplatexPLDAO()
{

}

bool TemplatexPLDAO::load(const QString &fileName)
{
    loaded = false;
    document = QDomDocument("xpl_message_templates");
    QFile file(fileName);
    if (!file.open(QIODevice::ReadOnly)) {
        lastError = "Failed to open file \""+fileName+"\"";
        return false;
    }
    if (!document.setContent(&file)) {
        file.close();
        lastError = "Failed to associate dom document to file \""+fileName+"\"";
        return false;
    }
    file.close();
    rootElement = document.documentElement();
    if(rootElement.nodeName() != "xpl-template") {
        document.clear();
        lastError = "Invalid tag \""+rootElement.nodeName()+"\"name for root node of the document";
        return false;
    }
    loaded = true;
    return true;
}



QList<TemplatexPLSchema> TemplatexPLDAO::getSchemas()
{
    if(!loaded) {
        return QList<TemplatexPLSchema>();
    }


    QList<TemplatexPLSchema> result;

    QDomNode schemaNode = rootElement.firstChild();
    while(!schemaNode.isNull()) {
        QDomElement schemaElement = schemaNode.toElement();
        QString schemaName = schemaElement.attribute("name", "");
        if(schemaName.isEmpty()) {
            schemaNode = schemaNode.nextSibling();
            continue;
        }
        TemplatexPLSchema schema(schemaName);
        QDomNode classNode = schemaNode.firstChild();
        while(!classNode.isNull()) {
            QDomElement classElement = classNode.toElement();
            QString className = classElement.attribute("name", "");
            if(className.isEmpty()) {
                classNode = classNode.nextSibling();
                continue;
            }
            bool command = (classElement.attribute("command", "false") == "true");
            bool status = (classElement.attribute("status", "false") == "true");
            bool trigger = (classElement.attribute("trigger", "false") == "true");
            TemplatexPLClass xpl_class(className, command, trigger, status);
            QDomNode itemNode = classNode.firstChild();
            while(!itemNode.isNull()) {
                QDomElement itemElement = itemNode.toElement();
                QString itemName = itemElement.attribute("name", "");
                if(!itemName.isEmpty()) {
                    itemNode = itemNode.nextSibling();
                    continue;
                }
                QString itemType = itemElement.attribute("type", "");
                QString itemDefaultValue = itemElement.attribute("default", "");
                TemplatexPLItem item(itemName, itemType, itemDefaultValue);
                xpl_class.items().append(item);
                itemNode = itemNode.nextSibling();
            }
            schema.classes().append(xpl_class);
            classNode = classNode.nextSibling();
        }
        result.append(schema);
        schemaNode = schemaNode.nextSibling();
    }
    return result;
}
